{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contextual GLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-contextual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.contextual import Contextual\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "# Set up the Contextual class with the required model and API key\n",
    "# Store the API key in a .env file as CONTEXTUAL_API_KEY\n",
    "load_dotenv()\n",
    "llm = Contextual(model=\"v1\", api_key=os.getenv(\"CONTEXTUAL_API_KEY\"))\n",
    "\n",
    "# Call the complete method with a query\n",
    "llm.complete(\n",
    "    \"Explain the importance of Grounded Language Models.\",\n",
    "    temperature=0.5,\n",
    "    max_tokens=1024,\n",
    "    top_p=0.9,\n",
    "    avoid_commentary=False,\n",
    "    knowledge=[\n",
    "        \"Contextual's Grounded Language Model (GLM) is the most grounded language model in the world. With state-of-the-art performance on FACTS (the leading groundedness benchmark) and our customer datasets, the GLM is the single best language model for RAG and agentic use cases for which minimizing hallucinations is critical. You can trust that the GLM will stick to the knowledge sources you give it\"\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.chat_engine.types import ChatMessage\n",
    "\n",
    "llm.chat([ChatMessage(role=\"user\", content=\"what color is the sky?\")])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
